package com.a3dweijing.wetouch2.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.a3dweijing.wetouch2.Activity.ImageShowActivity;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.ImageAdapter;
import com.a3dweijing.wetouch2.bean.ImageList;
import com.a3dweijing.wetouch2.bean.ImageMsg;
import com.a3dweijing.wetouch2.callback.OnClick;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.google.gson.Gson;

/**
 * Created by Yxd on 2017/10/31.
 */

public class ThreeDImageFragment extends Fragment implements OnClick {

    private RecyclerView imageRec;
    private Handler handler;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_threed_image,container,false);
        imageRec= (RecyclerView) view.findViewById(R.id.image_rec);
        imageRec.setLayoutManager(new LinearLayoutManager(getContext()));
        handler=new Handler()
        {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 100:
                        ImageMsg imageMsg=new Gson().fromJson(msg.obj.toString(),ImageMsg.class);
                        if (imageMsg!=null && imageMsg.getThreeDImgList()!=null && imageMsg.getThreeDImgList().size()>0){
                            imageRec.setAdapter(new ImageAdapter(imageMsg.getThreeDImgList(),ThreeDImageFragment.this));
                        }
                        break;
                    case 404:
                        break;
                    default:
                        break;
                }
            }
        };
        new GetImageThread("1","0").start();
        return view;
    }

    public static ThreeDImageFragment newinstance()
    {
        Bundle bundle=new Bundle();
        bundle.putString("name","图片");
        ThreeDImageFragment threeDSpecialFragment =new ThreeDImageFragment();
        threeDSpecialFragment.setArguments(bundle);
        return threeDSpecialFragment;
    }

    @Override
    public void onclick(int position, Object o) {
        ImageList imageList= (ImageList) o;
        Intent intent=new Intent(getContext(), ImageShowActivity.class);
        intent.putExtra("image",imageList);
        startActivity(intent);
    }

    private class GetImageThread extends Thread {
        String pageNo;
        String picturetype;

        public GetImageThread(String pageNo, String picturetype) {
            this.pageNo = pageNo;
            this.picturetype = picturetype;
        }

        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetImageData(pageNo,picturetype);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(100, resulet));
            } else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }


    }


}
